/*
 * Licensed to the 南京汇海交通科技有限公司
 *
 * Copyright (c) 2025.
 * All rights reserved.
 *
 * 本软件属南京汇海交通科技有限公司所有，在未获得南京汇海交通科技有限公司正式授权情况下，任何企业和个人，不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。
 */
package com.cje.basic.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chframework.common.exception.DaoException;
import com.chframework.common.thread.wrapper.BenchmarksFuture;
import com.chframework.mybatis.CommonDAO;
import com.cje.basic.dao.mapper.DictDataDOMapper;
import com.cje.basic.dataobject.DictDataDO;
import com.github.pagehelper.IPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@SuppressWarnings("resource")
public class DictDataDAO extends CommonDAO<DictDataDOMapper, DictDataDO> {

    /**
     * 分页查询字典数据列表
     */
    public BenchmarksFuture<PageInfo<DictDataDO>> pageDictData(IPage page, DictDataDO dictDataDO) {
        return BenchmarksFuture.supplyAsync(() -> {
            LambdaQueryWrapper<DictDataDO> queryWrapper = Wrappers.<DictDataDO>lambdaQuery()
                    .eq(DictDataDO::getStatus, 0);

            if (dictDataDO != null && StringUtils.isNotEmpty(dictDataDO.getDictType())) {
                queryWrapper.eq(DictDataDO::getDictType, dictDataDO.getDictType());
            }
            return queryWrapper;
        }).thenApplyAsync(queryWrapper -> {
            PageHelper.<DictDataDO>startPage(page);
            List<DictDataDO> list = super.list(queryWrapper);
            return new PageInfo<>(list);
        }).exceptionally(throwable -> {
            throw new DaoException(throwable.getCause());
        });
    }

    /**
     * 查询指定字典类型的字典配置项数据
     */
    public BenchmarksFuture<List<DictDataDO>> listDictData(String dictType) {
        return BenchmarksFuture.supplyAsync(() -> {
            LambdaQueryWrapper<DictDataDO> queryWrapper = Wrappers.<DictDataDO>lambdaQuery()
                    .eq(DictDataDO::getStatus, 0)
                    .eq(DictDataDO::getDictType, dictType);
            return super.list(queryWrapper);
        }).exceptionally(throwable -> {
            throw new DaoException(throwable.getCause());
        });
    }

    /**
     * 批量新增字典配置项数据
     */
    public BenchmarksFuture<Boolean> postDictDataBatch(List<DictDataDO> list) {
        return BenchmarksFuture.supplyAsync(() ->
                super.saveBatch(list)
        ).exceptionally(throwable -> {
            throw new DaoException(throwable.getCause());
        });
    }

    /**
     * 删除指定字典类型关联的字典数据列表
     */
    public BenchmarksFuture<Boolean> deleteDictData(String dictType) {
        return BenchmarksFuture.supplyAsync(() -> {
            LambdaQueryWrapper<DictDataDO> queryWrapper = Wrappers.<DictDataDO>lambdaQuery()
                    .eq(DictDataDO::getDictType, dictType);
            return super.remove(queryWrapper);
        }).exceptionally(throwable -> {
            throw new DaoException(throwable.getCause());
        });
    }

    /**
     * 修改字典选项数据
     */
    public BenchmarksFuture<Boolean> putDictData(DictDataDO dictDataDO) {
        return BenchmarksFuture.supplyAsync(() ->
                super.updateById(dictDataDO)
        ).exceptionally(throwable -> {
            throw new DaoException(throwable.getCause());
        });
    }

}
